classdef BandPassRauch < Cell
    properties
        wp;
        Q;
        K;
        n;
        m;
        k;
    end
    
    methods
        function this = BandPassRauch(poles, g)
            this.Name = 'Rauch BandPass ';
            this.Poles = poles;
            this.Gain = g;
            wp = abs(poles(1));
            this.wp = wp;
            [alpha ~] = cart2pol(real(poles(1)), imag(poles(1)));
            this.Q = abs(1./(2*sin(abs(alpha-pi/2))));
            if this.Q < 3
                this.ImagePath = 'Stages Schematics/RA_BP_Qc.png';
            else
                this.ImagePath = 'Stages Schematics/RA_BP_Qm.png';
            end
            this.calcCell(wp,this.Q,g);
        end
        
        function calcCell(obj, wp, Q, g)
            Qaux = [1/3,0.3,0.2,0.1];

            for l = 1: length(Qaux)
                Q0 = Q*Qaux(l);
                obj.K = 1 + (1/(2*Q0^2))*(1-Q0/Q);
                obj.m = g/(2*obj.K*Q*Q0);
                if (obj.m < 1)
                    obj.error = 0;
                    break
                end
            end
            
            if ~obj.error
                if wp < 100
                    caux = 56e-9;
                else
                    if wp < 100000
                        caux = 5.6e-9;
                    else
                        if wp < 1000000
                            caux = 560e-12;
                        else
                            caux = 56e-12;
                        end
                    end
                end
            end
            
            cap(1) = caux;
            cap(2) = caux;
            
            if obj.Q < 3
                resist(1) = Q/(g*wp*caux);
                resist(2) = 2*Q/(wp*caux);
                resist(3) = Q/((2*Q^2-g)*wp*caux);
            else
                G = (caux*wp)*(2*Q0);

                resist(1) = 1/(4*obj.m*Q0^2*G);
                resist(2) = 1/G;
                resist(3) = 1/((1-obj.m)*4*Q0^2*G);
                resist(5) = 1e3;
                resist(4) = resist(5)/(obj.K-1);
            end
            
            obj.Resists = resist;
            obj.Caps = cap;
            
            obj.sensis();
        end
        
        function componentChange(obj, component, value)
            caps = obj.Caps;
%             val=false;
            resists = obj.Resists;
            
%             if (val)
            if obj.Q < 3
                switch component
                    case 1
                        obj.Resists(1) = value;
                        raux = obj.Resists(1)*(1 - obj.k);
                        obj.Resists(2) = raux/obj.k;
                        obj.Resists(3) = raux*(obj.K - 1);
                    case 2
                        obj.Resists(2) = value;
                        raux = obj.Resists(2)*obj.k;
                        obj.Resists(1) = raux/(1 - obj.k);
                        obj.Resists(3) = raux*(obj.K - 1);
                    case 3
                        obj.Resists(3) = value;
                        raux = value/(obj.K - 1);
                        obj.Resists(1) = raux/(1 - obj.k);
                        obj.Resists(2) = raux/obj.k;
                    case 4
                        obj.Caps(1) = value;
                        obj.Caps(2) = value;
                        obj.Resists(1) = obj.Q/(obj.Gain*obj.wp*value);
                        obj.Resists(2) = 2*obj.Q/(obj.wp*value);
                        obj.Resists(3) = obj.Q/((2*obj.Q^2-obj.Gain)*obj.wp*value);
                    case 5
                        obj.Caps(1) = value;
                        obj.Caps(2) = value;
                        obj.Resists(1) = obj.Q/(obj.Gain*obj.wp*value);
                        obj.Resists(2) = 2*obj.Q/(obj.wp*value);
                        obj.Resists(3) = obj.Q/((2*obj.Q^2-obj.Gain)*obj.wp*value);
                end
            else
            end
%             end
            status = obj.validateComponents(obj.Resists,obj.Caps);
            
            if (~status)
                obj.Caps = caps;
                obj.Resists = resists;
                obj.sensis();
            end
            
        end
        
        function sensis(obj)
            
            R1=obj.Resists(1);
            R2=obj.Resists(2);
            R3=obj.Resists(3);
            if (obj.Q>=3)
                R4=obj.Resists(4);
                R5=obj.Resists(5);
            else
                R4=999e6;
                R5=0;
            end
            C1=obj.Caps(1);
            C2=obj.Caps(2);

            obj.Senswp(1)=-R3/(2*(R1 + R3));
            obj.Senswp(2)=-1/2;
            obj.Senswp(3)=-R1/(2*(R1 + R3));
            obj.Senswp(4)=0;
            obj.Senswp(5)=0;
            obj.Senswp(6)=-1/2;
            obj.Senswp(7)=-1/2;

            obj.SensG(1)=(-(C2*R2*R3*R5)/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5) - 1);
            obj.SensG(2)=(R1*R3*R4*(C1 + C2))/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5);
            obj.SensG(3)=-(C2*R1*R2*R5)/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5);
            obj.SensG(4)=-(R4*R5*(C1*R1*R3 + C2*R1*R2 + C2*R1*R3 + C2*R2*R3))/((R4 + R5)*(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5));
            obj.SensG(5)=(R4*R5*(C1*R1*R3 + C2*R1*R2 + C2*R1*R3 + C2*R2*R3))/((R4 + R5)*(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5));
            obj.SensG(6)=-(C1*R1*R3*R4)/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5);
            obj.SensG(7)=(C1*R1*R3*R4)/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5);

            obj.SensQ(1)=-(R1*(C1*R3^2*R4 + C2*R3^2*R4 + C2*R2*R3*R5) + C2*R2*R3^2*R5)/(2*C1*R1*R3^2*R4 + 2*C1*R1^2*R3*R4 + 2*C2*R1*R3^2*R4 - 2*C2*R1^2*R2*R5 + 2*C2*R1^2*R3*R4 - 2*C2*R2*R3^2*R5 - 4*C2*R1*R2*R3*R5);
            obj.SensQ(2)=(C2*R1*R2*R5 + C2*R2*R3*R5)/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5) + 1/2;
            obj.SensQ(3)=-(R3*(C1*R1^2*R4 + C2*R1^2*R4 + C2*R1*R2*R5) + C2*R1^2*R2*R5)/(2*C1*R1*R3^2*R4 + 2*C1*R1^2*R3*R4 + 2*C2*R1*R3^2*R4 - 2*C2*R1^2*R2*R5 + 2*C2*R1^2*R3*R4 - 2*C2*R2*R3^2*R5 - 4*C2*R1*R2*R3*R5);
            obj.SensQ(4)=-(C2*R2*R5*(R1 + R3))/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5);
            obj.SensQ(5)=(C2*R2*R5*(R1 + R3))/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5);
            obj.SensQ(6)=- (C2*R1*R2*R5 - C2*R1*R3*R4 + C2*R2*R3*R5)/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5) - 1/2;
            obj.SensQ(7)=(C2*R1*R2*R5 - C2*R1*R3*R4 + C2*R2*R3*R5)/(C1*R1*R3*R4 - C2*R1*R2*R5 + C2*R1*R3*R4 - C2*R2*R3*R5) + 1/2;
        end
    end
end